package com.hengxinguotong.hxgtwygsp.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.hengxinguotong.hxgtwygsp.model.User;

/**
 * 用户账户处理类
 */
public class UserUtil
{
    private static final String USER = "user";
    private static final String KEY = "xXO00o1=";

    private PreferencesUtil preferencesUtil;
    private Gson gson;

    public UserUtil(PreferencesUtil preferencesUtil, Gson gson)
    {
        this.preferencesUtil = preferencesUtil;
        this.gson = gson;
    }

    /**
     * 获取当前用户
     *
     * @return com.hengxinguotong.hxgtwyglj.model.User
     * @author hjz
     * @since 2018/12/22 16:20
     */
    public User getUser()
    {
        User user = null;
        String userInfo = preferencesUtil.queryData(USER);
        if (!TextUtils.isEmpty(userInfo))
        {
            String userJson = Algorithm.DesDecrypt(userInfo, KEY);
            user = gson.fromJson(userJson, User.class);
        }
        return user;
    }

    /**
     * 保存用户
     *
     * @param user
     * @return void
     * @author hjz
     * @since 2018/12/22 16:20
     */
    public void saveUser(User user)
    {
        String userJson = gson.toJson(user);
        String userInfo = Algorithm.DesEncrypt(userJson, KEY);
        preferencesUtil.saveData(USER, userInfo);
    }

    /**
     * 清除用户
     *
     * @return void
     * @author hjz
     * @since 2018/12/22 16:20
     */
    public void removeUser()
    {
        preferencesUtil.remove(USER);
    }
}
